﻿using Pay.Core;
using Pay.Data;
using Pay.DTO;
using Pay.IService;
using Pay.Service.Cache;
using Pay.TO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Pay.Service {
   public class QuotaRecordService : ServiceBase, IQuotaRecordService, IDisposable {
        private IUnitOfWork _iUnitOfWork;
        private ITokenStatisticsService _iTokenStatisticsService;
        public QuotaRecordService(IUnitOfWork iUnitOfWork, ITokenStatisticsService iTokenStatisticsService) : base(iUnitOfWork) {
            _iUnitOfWork = iUnitOfWork;
            _iTokenStatisticsService = iTokenStatisticsService;
        }
        public quota_record_dto Get(long id) {
            var model = context.QuotaRecord.FindById(id).ToDtoObject<quota_record_dto>();
            return model;
        }

        public PagedResult<quota_record_dto> Gets(QuotaQuery query) {
            var filter = PredicateBuilder.True<QuotaRecord>();
            if (!string.IsNullOrEmpty(query.name))
                filter = filter.And(c => c.MerchantName.Contains(query.name)||c.Merchant.Mobile.Contains(query.name));
            if (query.start_time.HasValue) {
                long startTime = query.start_time.Value.ToUnix();
                filter = filter.And(c => c.ApplyTime >= startTime);
            }
            if (query.end_time.HasValue) {
                long endTime = query.end_time.Value.ToUnix();
                filter = filter.And(c => c.ApplyTime <= endTime);
            }
            if (query.equota_record_audit.HasValue) {
                filter = filter.And(c => c.EQuotaRecordAudit == (EQuotaRecordAudit)query.equota_record_audit.Value);
            }
            return context.QuotaRecord.FromCachePaged<QuotaRecord, quota_record_dto>(query.page, query.size, filter, orderBy => orderBy.OrderByDescending(c => c.ApplyTime));
        }

        public bool Save(quota_record_dto dto) {
            if (dto.id == 0) {
                var quota = dto.ToDtoObject<QuotaRecord>();
                quota.ApplyTime = DateTime.Now.ToUnix();
                context.QuotaRecord.Add(quota);
            } 
            int r = _iUnitOfWork.Commit();
            return r > 0;
        }
        /// <summary>
        /// 邀请记录审核
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool Audit(quota_audit_model model) {
            var quota = context.QuotaRecord.FindById(model.id);
            if (quota.EQuotaRecordAudit != EQuotaRecordAudit.PendingAudit) {
                throw new ServerErrorException("该记录已审核");
            }
            if (model.audit_num <= 0) { throw new ServerErrorException("添加数量不能小于0"); }
            if (model.equota_record_audit < 0) { throw new ServerErrorException("请选择审核状态"); }
            quota.EQuotaRecordAudit = (EQuotaRecordAudit)model.equota_record_audit;
            quota.AuditNum = model.audit_num;
            quota.Remark = model.remark;
            quota.AuditTime= DateTime.Now.ToUnix();
            if (quota.EQuotaRecordAudit == EQuotaRecordAudit.Passed) {
                var statistic = context.TokenStatistics.FirstOrDefault(s => s.MerchantId == quota.MerchantId);
                if (statistic == null) {
                     statistic = new TokenStatistics() { MerchantId = quota.MerchantId, CreateTime = DateTime.Now.ToUnix() };
                }
                statistic.TotalOpenMaker += quota.AuditNum.Value;
                statistic.TotalSurplusOpenMaker += quota.AuditNum.Value;
            }
          
            
            int r = _iUnitOfWork.Commit();
            return r > 0;
        }
    }
}
